package org.fanpeng.demo.design;

import java.util.concurrent.atomic.AtomicReference;

/**
 * className: Singleton_00
 * Description:
 * date: 2021/8/4 11:13
 *
 * @author fanpeng
 * @version 1.0
 */
public class Singleton_06 {
    // CAS「AtomicReference」(线程安全)
    private static final AtomicReference<Singleton_06> INSTANCE = new AtomicReference<Singleton_06>();

    private Singleton_06(){
    }

    private static Singleton_06 instance;

    public static final Singleton_06 getInstance() {
        for (;;){
            Singleton_06 instance = INSTANCE.get();
            if (null != instance) {
                return instance;
            }
            INSTANCE.compareAndSet(null, new Singleton_06());
            return INSTANCE.get();
        }
    }

    public static void main(String[] args) {
        // org.fanpeng.demo.design.Singleton_06@4b67cf4d
        System.out.println(Singleton_06.getInstance());
        // org.fanpeng.demo.design.Singleton_06@4b67cf4d
        System.out.println(Singleton_06.getInstance());
    }


}
